package com.example.srm.auth.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.srm.auth.service.TUserService;
import com.example.srm.constant.PermissionConstant;
import com.example.srm.domain.TRole;
import com.example.srm.domain.TUser;
import com.example.srm.auth.mapper.TUserMapper;
import com.example.srm.domain.tree.Tree;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
*
*/
@Service
public class TUserServiceImpl extends ServiceImpl<TUserMapper, TUser> implements TUserService {

    @Autowired
    private TUserMapper userMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public List<Tree> getPermissionById(String id) {
        //通过用户id获取权限树
        Set<Tree> permissionById = userMapper.getPermissionById(id);
        System.out.println(permissionById);
        for (Tree tree : permissionById) {
            tree.setIndex(tree.getCode().split(":|[.]")[1]);
        }
        return toTree(permissionById.stream().collect(Collectors.toList()));
    }

    @Override
    public List<Tree> getPermissionTreeById(String id) {
        Set<Tree> listTree = userMapper.listTree(id);
        System.out.println("listTree:==================>"+listTree);
        List<Tree> trees = toTree(listTree.stream().collect(Collectors.toList()));
        System.out.println("trees______>"+trees);
        for (Tree tree : trees) {
            tree.setIndex(tree.getCode().split(":")[1]);
        }
        return trees;
    }

    @Override
    public List<TRole> getRolesByUserId(String id) {
        return userMapper.getRolesByUserId(id);
    }

    @Override
    public boolean updatePermissionForRedis(String userId) {
        try {
            //更新redis里的权限
            Set<Tree> permissionById = userMapper.listTree(userId);
            List<String> collect = permissionById.stream().map(e -> e.getCode()).collect(Collectors.toList());
            redisTemplate.opsForHash().put(PermissionConstant.USER_AUTH,userId,collect);
        }catch (Exception e){
            return false;
        }
        return true;
    }

    @Override
    public TUser getUserByToken(String token) {

        return userMapper.getUserByToken(token);
    }

    /**
     * 将树结点对象集合转为树结构集合
     * @param list
     * @return
     */
    private List<Tree> toTree(List<Tree> list) {
        HashMap<String, Tree> map = new HashMap<>();
        list.forEach(e->map.put(e.getId(),e));

        List<Tree> list1 = new ArrayList<>();

        for (Tree tree : list) {
            Tree parent = map.get(tree.getPid());
            if (parent==null){
//                System.out.println(tree.getName()+","+tree.getIndex());
                //解决前端的bug，需要在根节点设置 false
                tree.setChecked(false);
                list1.add(tree);
            }else {
                if (parent.getChildren()==null){
                    parent.setChildren(new LinkedList<>());
                }
//                System.out.println(tree.getName()+","+tree.getIndex());
                parent.getChildren().add(tree);
            }
        }
        return list1;
    }

}
